package algorithm

import scala.collection.mutable.Map;
import scala.util.control.Breaks._;

object TwoNumsSum {
  def twoSum(nums: Array[Int], target: Int): Array[Int] = {


    var map4ArrayIndex : Map[Int,Int] = Map[Int,Int]();
    for(i <- 0 to nums.length-1){
      map4ArrayIndex += (nums(i) -> i);map4ArrayIndex.get(1);
    }
    for(i <- 0 to nums.length-1){
      val valInMap = map4ArrayIndex.get(target-nums(i));
      if( valInMap.nonEmpty && valInMap.get!=i){
        return Array[Int](i,valInMap.get);
      }
    }
    return Array[Int](-1,-1);
  }

  def twoSum2(nums: Array[Int], target: Int): Array[Int] = {

    var map4ArrayIndex : Map[Int,Int] = Map[Int,Int]()
    for(i <- 0 to nums.length-1){
      if( map4ArrayIndex.contains(target-nums(i))){
        return Array[Int](i,map4ArrayIndex.get(target-nums(i)).get)
      }
      map4ArrayIndex += (nums(i) -> i)
    }
    return Array[Int](-1,-1);
  }

  def main(args: Array[String]): Unit = {
    twoSum2(Array(3,2,4),6).foreach(println(_))
  }

}
